const app=getApp()
const DrawUser=wx.cloud.database().collection('DrawUser')
const CreditRecord=wx.cloud.database().collection('CreditRecord')
Page({
    data: {
        ischecked: false,
        userinfo:{},
        
    },
    onLoad(){
        let ischecked=wx.getStorageSync('ischecked') || false
        let userinfo=wx.getStorageSync('userinfo')
        app.globalData.userInfo=userinfo
        app.globalData.ischecked=ischecked
        if(ischecked){
            this.setData({
                ischecked:true,
                userinfo
            })
        }
        wx.setStorageSync('app.globalData.userInfo', userinfo)
        wx.setStorageSync('app.globalData.ischecked', ischecked)
    },

    showMyDraw(){
        wx.navigateTo({
          url: '../drawlog/drawlog',
        })

    },

    showMyCredit(){
        wx.navigateTo({
          url: '../creditlog/creditlog',
        })

    },

    get(){
        let that=this;
        wx.getUserProfile({
             
            desc: '获取用户信息',
            success(res) {
                let userinfo=res.userInfo
                console.log(res)
                that.setData({
                    ischecked:true,
                    userinfo
                })
                app.globalData.userInfo=userinfo
                app.globalData.ischecked=!app.globalData.ischecked
                wx.setStorageSync('ischecked', true),
                wx.setStorageSync('userinfo', userinfo),
                wx.setStorageSync('app.globalData.userInfo', userinfo),
                wx.setStorageSync('app.globalData.ischecked', true),
                wx.login({
                    //获取code
                    success: function (res) {
                      var code = res.code; //返回code
                      console.log(code);
                      var appId = 'wx71167587709a106e';//微信小程序AppID
                      var secret = '4272f703c6b9f38c6bd6b12de9cca684';//可在微信公众平台设置扫描二维码获取
                      wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appId+'&secret='+secret+'&js_code='+code+'&grant_type=authorization_code',
                        data: {},
                        header: { 
                          'content-type': 'json'
                        },   
                        success: function (e) {
                          var openid = e.data.openid //返回openid
                          console.log(openid)//控制台打印openid
                          app.globalData.userOpenId = openid;
                          wx.setStorageSync('app.globalData.userOpenId', openid)
                          console.log(111)
                          console.log(app.globalData.userOpenId)
                          DrawUser.where({_openid:app.globalData.userOpenId}).count().then(e=>{
                        
                            console.log(e.total)
                            if(!e.total){
                                DrawUser.add({
                                    data:{
                                        credit:0,
                                        ticket:0,
                                    }
                                })
                            }
                          
                        })
                        
                        }


                      })
                    }
                  })

                  //若DrawUser内无用户记录，则创建一个唯一记录
                  
                  //console.log(DrawUser.where({_openid:"oAXLj4nnMH2PvKFNV3ZTcnhPbuu0"}).get())
                  
            },
            fail() {
                wx.showToast({
                    title: '请允许！',
                    icon: 'none'
                })

            },
        })
    }
})